<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

/**
 * 商品评论模型
 */
class GoodsComment extends Model
{
    // 设置表名
    protected $name = 'goods_comment';

    // 自动写入时间
    protected $autoWriteTimestamp = true;

    // 评论状态：待审核
    const STATUS_PENDING = 0;
    // 评论状态：已通过
    const STATUS_APPROVED = 1;
    // 评论状态：已拒绝
    const STATUS_REJECTED = 2;

    /**
     * 关联商品
     */
    public function goods()
    {
        return $this->belongsTo(Goods::class, 'goods_id');
    }

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * 关联父评论（回复）
     */
    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    /**
     * 关联子评论（回复）
     */
    public function replies()
    {
        return $this->hasMany(self::class, 'parent_id')->where('status', self::STATUS_APPROVED);
    }

    // 搜索器
    public function searchStatusAttr($query, $value, $data)
    {
        if ($value !== '') {
            $query->where('status', $value);
        }
    }

    // 商品ID搜索器
    public function searchGoodsIdAttr($query, $value, $data)
    {
        if ($value !== '') {
            $query->where('goods_id', $value);
        }
    }

    // 用户ID搜索器
    public function searchUserIdAttr($query, $value, $data)
    {
        if ($value !== '') {
            $query->where('user_id', $value);
        }
    }

    // 父评论ID搜索器
    public function searchParentIdAttr($query, $value, $data)
    {
        if ($value !== '') {
            $query->where('parent_id', $value);
        }
    }
}